package com.yaowk.system.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.activerecord.tx.TxConfig;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.I18nKit;
import com.yaowk.common.kit.ResultKit;
import com.yaowk.common.validator.RequiredIdValidator;
import com.yaowk.system.model.AdvertisingSpace;
import com.yaowk.system.validator.NoValidator;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.Date;
import java.util.List;

/**
 * 广告管理
 *
 * @authc yaowk
 * 2017/4/25
 */
@TxConfig(Constant.SYSTEM_MODULE)
public class AdvertisingController extends Controller {

    /**
     * 查询广告位列表
     *
     * @param id|平台id|Integer|必填
     * @title 查询广告位列表
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("system:read")
    public void list() {
        Integer id = getParaToInt("id");
        List<AdvertisingSpace> advertisingSpace = AdvertisingSpace.dao.findByPlatformId(id);
        renderJson(ResultKit.success(advertisingSpace));
    }

    /**
     * 添加广告位
     *
     * @param platform_id|平台id|Integer|是
     * @param name|名称|String|否
     * @param title|默认广告标题|String|否
     * @param url|默认广告链接|String|否
     * @param image|默认广告图片|String|否
     * @title 添加广告位
     */
    @Before({ POST.class, Tx.class })
    @RequiresPermissions("system:update")
    public void add() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        AdvertisingSpace advertisingSpace = getModel(AdvertisingSpace.class, "");
        advertisingSpace.setCreateTime(new Date())
                .setCreateUser(user.getId())
                .save();
        advertisingSpace.setNo(String.valueOf(advertisingSpace.getId())).update();
        renderJson(ResultKit.success(I18nKit.get("add_success", Constant.SYSTEM_MODULE, this)));
    }

    /**
     * 编辑广告位
     *
     * @param id|广告id|Integer|必填
     * @param platform_id|平台id|Integer|是
     * @param name|名称|String|否
     * @param title|默认广告标题|String|否
     * @param url|默认广告链接|String|否
     * @param image|默认广告图片|String|否
     * @title 编辑广告位
     */
    @Before({ POST.class, RequiredIdValidator.class, NoValidator.class })
    @RequiresPermissions("system:update")
    public void edit() {
        AdvertisingSpace advertisingSpace = getModel(AdvertisingSpace.class, "");
        advertisingSpace.update();
        renderJson(ResultKit.success(I18nKit.get("edit_success", Constant.SYSTEM_MODULE, this)));
    }

    /**
     * 删除广告
     *
     * @param id|广告id|Integer|必填
     * @title 删除广告
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("system:update")
    public void remove() {
        Integer id = getParaToInt("id");
        AdvertisingSpace advertisingSpace = AdvertisingSpace.dao.findById(id);
        advertisingSpace.setStatus(0).update();
        renderJson(ResultKit.success(I18nKit.get("remove_success", Constant.SYSTEM_MODULE, this)));
    }
}
